
fetch("https://music.0melon0.cn/search?keywords=晴天", {
    method: "GET",// type请求类型
}).then(function (response) {
    return response.json(); //转json格式返回值，解析为可读数据
}).then(function (data) {
    console.log(data); //获取请求成功的结果
    let send = document.querySelector("#send");

    let tbody = document.querySelector("tbody");
    //搜索
    send.addEventListener("click", function () {

        console.log(11);
        let seach = document.querySelector("#keywords");
        let seachVal = seach.value;
        console.log(seachVal);
        fn1 ();
    })
function fn1 (){
    data.result.songs.forEach(function (item, idx) {
        // 渲染
        let min = moment(item.duration).format('MM:SS')
        tbody.innerHTML += `<tr>
        <td>${item.name}</td>
        <td>${item.artists[0].name}</td>
        <td>${item.album.name}</td>
        <td>${min}</td>  
        </tr>`;
    });
    // 播放
    let tr = document.querySelectorAll("tr");
    tr.forEach(function (el, ide) {
        el.onclick = function () {
            console.log(data.result.songs[ide - 1].id);
            fetch(`https://music.0melon0.cn/song/url?id=${data.result.songs[ide - 1].id}`, {
                method: "GET",// type请求类型
            }).then(function (response) {
                return response.json(); //转json格式返回值，解析为可读数据
            }).then(function (data) {
                let musicss=document.querySelector(".musicss");
                let audio=document.querySelector("audio");
                musicss.removeChild(audio);
                console.log(data.data[0].url); //获取请求成功的结果
                musicss.innerHTML = `<audio id="player" src="${data.data[0].url}" controls autoplay></audio>`;
            }).catch(function (error) {
                console.log(error); //获取请求失败的结果
            });
        }
    });
}

}).catch(function (error) {
    console.log(error); //获取请求失败的结果
});
